package com.antony.messageUser1018;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

import javax.swing.JList;
import javax.swing.JTextArea;

public class ChatListener extends MouseAdapter implements ActionListener {

	private Socket sk;
	private JTextArea taSentMsg;
	private String target;

	public ChatListener(Socket sk, JTextArea taSentMsg) {
		this.sk = sk;
		this.taSentMsg = taSentMsg;
	}

	public void mouseClicked(MouseEvent e) {
		if (e.getClickCount() == 2) {
			JList list = (JList) e.getSource();
			target = list.getSelectedValue().toString();
			System.out.println(target);
			ShowFrame frame = new ShowFrame(sk);
			frame.showChatFrame(target);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String sentMsg = taSentMsg.getText();
		if (!sentMsg.equals("")) {
			try {
				OutputStream out = sk.getOutputStream();
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
				bw.write("sentMessage|" + target + "|" + sentMsg + "\r\n");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}
